<?php
class Backend_Designer_Controller extends Backend_Controller
{
	/**
	 * Designer config
	 * @var Config_File_Array
	 */
	protected $_config;
	/**
	 * Source scripts
	 * @var array
	 */
	static protected $_scripts = array(	
		'/js/app/system/crud/designer.js',
		'/js/app/system/SearchPanel.js',
		'/js/app/system/designer/application.js',
		'/js/app/system/designer/connections.js',
		'/js/app/system/designer/configWindow.js',
		'/js/app/system/designer/configWindow.js',
		'/js/app/system/designer/objects/tree.js',
		'/js/app/system/designer/objects.js',
		'/js/app/system/designer/properties.js',
		'/js/app/system/FilesystemWindow.js',
		'/js/app/system/crud/designer.js'
	);
	
	
	public function __construct()
	{
		parent::__construct();
		$this->_config = $config = Config::factory(Config::File_Array, Application::getConfig()->get('configs').'designer.php');
	}
	
	public function indexAction()
	{
	    $this->_resource->addJs('/js/app/system/designer/lang/'.$this->_config->get('lang').'.js'  , 1);
		$this->_resource->addCss('/js/app/system/designer/style.css' );	
		if(!$this->_config->get('development'))
			$this->_resource->addCss('/js/app/system/designer.js' );
		else
			foreach (self::$_scripts as $path)
				$this->_resource->addJs($path , 2);
	}
	
	public function subAction()
	{
		$subController = Request::getInstance()->getPath(3);
		$subAction = Request::getInstance()->getPath(4);
		if($subController === false || !strlen($subController) || $subAction===false || !strlen($subAction))
			Response::jsonError($this->_lang->WRONG_REQUEST);
			
		$subController = 'Backend_Designer_Sub_'.ucfirst(Filter::filterValue('pagecode',$subController));
		$subAction = Filter::filterValue('pagecode',$subAction).'Action';

		if(!class_exists($subController) || !method_exists($subController, $subAction))
			Response::jsonError($this->_lang->WRONG_REQUEST);
			
		$sub = new $subController();
		$sub->$subAction();	
		
		exit();
	}
}